package com.wondream.myframework.app.mybatis.plugin;

import org.mybatis.generator.api.GeneratedXmlFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;

import java.util.List;

public class MapperClassNamePlugin extends PluginAdapter {
    @Override
    public boolean validate(List<String> list) {
        return true;
    }

    @Override
    public void initialized(IntrospectedTable table) {
        super.initialized(table);
        String name = table.getMyBatis3JavaMapperType();
        if(name.endsWith("DOMapper")) {
            table.setMyBatis3JavaMapperType(name.replaceAll("DOMapper$", "Dao"));
        } else {
            table.setMyBatis3JavaMapperType(name.replaceAll("Mapper$", "Dao"));
        }
        String xmlName = table.getMyBatis3XmlMapperFileName();
        if(xmlName.endsWith("DOMapper.xml")){
            table.setMyBatis3XmlMapperFileName(xmlName.replaceAll("DOMapper.xml", "Mapper.xml"));
        }
    }

    @Override
    public boolean clientUpdateByPrimaryKeySelectiveMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
        return true;
    }

    @Override
    public List<GeneratedXmlFile> contextGenerateAdditionalXmlFiles(IntrospectedTable introspectedTable) {
        return null;
    }
}
